﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_PERMISSION.Base
{
    public class Permission
    {
        protected string userName;
        protected string passWord;

        private string[] temp;
        private int[] roles;

        public Permission()
        {
            userName = string.Empty;
            passWord = string.Empty;
            //string[] temp = new string[2];
        }

        public bool Is_Login()
        {
            bool ret = true;

            try
            {
                this.temp = Utilities.ReadFromFile(PermissionConstants.FileName);
                if (this.temp.Length > 1)
                {
                    if (!string.IsNullOrEmpty(this.temp[0]) && !string.IsNullOrEmpty(this.temp[1]))
                    {
                        this.userName = Utilities.Decrypt(PermissionConstants.PasswordHash, PermissionConstants.SaltKey, PermissionConstants.VIKey, this.temp[0]);
                        this.passWord = Utilities.Decrypt(PermissionConstants.PasswordHash, PermissionConstants.SaltKey, PermissionConstants.VIKey, this.temp[1]);
                        if (!string.IsNullOrEmpty(this.userName) && !string.IsNullOrEmpty(this.passWord))
                        {
                            if (VerifyInfo(this.userName, this.passWord))
                            {
                                roles = this.GetRole();
                            }
                            else
                            {
                                ret = false;
                            }
                        }
                        else
                        {
                            ret = false;
                        }
                    }
                    else
                    {
                        ret = false;
                    }
                }
                else
                {
                    ret = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return ret;
        }

        private int[] GetRole()
        {
            int[] array;
            var parameters = new SqlParameter[]
                    {
                        BaseDAO.CreateParameter("@UserName", SqlDbType.NVarChar, this.userName)
                    };
            DataTable dt = BaseDAO.ExcuteProcedure("Sp_222_GetRole", parameters);
            array = new int[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                array[i] = int.Parse(dt.Rows[i]["FunctionID"].ToString());
            }

            return array;
        }

        private bool VerifyInfo(string user, string pass)
        {
            bool result = false;
            string sql = "Select * from [222_Users] where [UserName] = N'" + user + "' AND [Password] = N'" + pass + "'";
            if (BaseDAO.ExecuteQuery(sql).Rows.Count > 0)
            {
                result = true;
            }

            return result;
        }

        public static bool CheckLogin(string user, string pass)
        {
            bool result = false;
            string encryptPass = Utilities.EncryptPassword(pass, PermissionConstants.Key);
            string sql = "Select * from [222_Users] where [UserName] = N'" + user + "' AND [Password] = N'" + encryptPass + "'";
            if (BaseDAO.ExecuteQuery(sql).Rows.Count > 0)
            {
                result = true;
            }

            return result;
        }

        public bool HasRight(int functionId)
        {

            if (roles != null && roles.Length > 0 && roles.Contains(functionId))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool HasNotRight(int functionId)
        {

            if (roles == null || roles.Length < 0 || !roles.Contains(functionId))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void Init()
        {
            this.temp = Utilities.ReadFromFile(PermissionConstants.FileName);
            this.userName = Utilities.Decrypt(PermissionConstants.PasswordHash, PermissionConstants.SaltKey, PermissionConstants.VIKey, this.temp[0]);
            this.passWord = Utilities.Decrypt(PermissionConstants.PasswordHash, PermissionConstants.SaltKey, PermissionConstants.VIKey, this.temp[1]);
            if (VerifyInfo(this.userName, this.passWord))
            {
                roles = this.GetRole();
            }
            else
            {
                return;
            }
        }

        public string UserName
        {
            get { return this.userName; }
            
        }

    }
}
